package com.hmyuan.controller.back;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Sys_resources;
import com.hmyuan.domain.Sys_role;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.service.SysResourcesService;
import com.hmyuan.service.SysRoleService;

/**
 * 系统角色controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/back/sysrole")
public class SysRoleController {

	@Resource
	SysRoleService sysRoleService;
	@Resource
	SysResourcesService sysResourcesService;
	/**
	 * 角色管理页面
	 */
	@RequestMapping("/sysrolePage")
	public String sysrolePage(HttpServletRequest request,Model model){
		return "/back/sysrole/sysrolePage";
	}
	/**
	 * 分页查询角色列表
	 */
	@RequestMapping("/findSysroleList")
	public String findSysroleList(HttpServletRequest request,Model model,Integer pageNum,Sys_role sys_role){
		if(pageNum==null) pageNum=1;
		request.setAttribute("pageInfo", sysRoleService.selectListByPage(sys_role, pageNum, 15));
		return "/back/sysrole/sysrolePageList";
	}
	/**
	 * 角色新增页面
	 */
	@RequestMapping("/sysroleAddPage")
	public String sysroleAddPage(HttpServletRequest request,Model model){
		return "/back/sysrole/sysroleAddPage";
	}
	/**
	 * 新增角色页面提交
	 */
	@ResponseBody
	@RequestMapping("/addsysrole")
	public String addsysrole(HttpServletRequest request,Model model,Sys_role sys_role){
		sys_role.setState("0");
		sysRoleService.insert(sys_role);
		return "success";
	}
	/**
	 * 编辑角色信息页面
	 */
	@RequestMapping("/sysroleUpdatePage")
	public String sysroleUpdatePage(HttpServletRequest request,Model model,Long id){
		if(null==id) return "/sysrole/sysroleAddPage";
		Sys_role sys_role = sysRoleService.selectByPrimaryKey(id);
		request.setAttribute("sys_role", sys_role);
		return "/back/sysrole/sysroleUpdatePage";
	}
	/**
	 * 编辑角色页面提交
	 */
	@ResponseBody
	@RequestMapping("/updatesysrole")
	public String updatesysrole(HttpServletRequest request,Model model,Sys_role sys_role){
		if(null==sys_role.getId()) return "fail";
		sysRoleService.updateByPrimaryKeySelective(sys_role);
		return "success";
	}
	/**
	 * 启用和禁用角色（3为删除状态）
	 */
	@ResponseBody
	@RequestMapping("/sysroleUpadatestate")
	public String sysroleUpadatestate(HttpServletRequest request,Model model,Long[] ids,Integer state){
		if(null==state||null==ids||ids.length==0) return "fail";
		Sys_role sys_role = new Sys_role();
		sys_role.setIds(ids);
		sys_role.setState(state+"");//修改状态
		sysRoleService.updateByPrimaryKeySelective(sys_role);
		return "success";
	}
	/**
	 * 用户资源权限分配页面
	 */
	@RequestMapping("/sysroleResourcesUpdatePage")
	public String sysroleResourcesUpdatePage(HttpServletRequest request,Model model,Long roleid){
		
		Sys_resources sys_resources = new Sys_resources();
		sys_resources.setIshide(0);//查询未被隐藏的数据
		List<Sys_resources> list = sysResourcesService.selectList(sys_resources);
		List<Sys_resources> rolereslist = sysResourcesService.selectListByRoleid(roleid);
		//将菜单遍历为树形结构
		List<Sys_resources> sysList = new ArrayList<>();
		for (Sys_resources sys : list) {
			for (Sys_resources sys2 : list) {
				if(sys.getParentid()!=null&&sys.getParentid()==sys2.getId()){
					if(sys2.getNodes()==null){sys2.setNodes(new ArrayList<Sys_resources>());}
					sys2.getNodes().add(sys);
				}
			}
			if(null!=sys.getLevel()&&sys.getLevel()==1){//判断是否为主目录
				sysList.add(sys);
			}
		}
		request.setAttribute("sysList", sysList);
		request.setAttribute("roleid", roleid);
		request.setAttribute("rolereslist", rolereslist);
		
		return "/back/sysrole/sysroleResourcesUpdatePage";
	}
	/**
	 * 用户资源权限分配提交
	 */
	@ResponseBody
	@RequestMapping("/sysroleResourcesUpdate")
	public String sysroleResourcesUpdate(HttpServletRequest request,Model model,Long[] resIds,Long roleid){
		if(null==roleid) return "fail";
		sysRoleService.insertResRoles(roleid, resIds);
		return "success";
	}
}
